package com.cskaoyan.service;

import com.cskaoyan.bean.Comment;
import com.cskaoyan.bean.CommentExample;
import com.cskaoyan.bean.Goods;
import com.cskaoyan.bean.GoodsExample;
import com.cskaoyan.mapper.CommentMapper;
import com.cskaoyan.vo.CommentListVo;
import com.cskaoyan.vo.GoodsListVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    CommentMapper commentMapper;

    @Override
    public CommentListVo list(Integer page, Integer limit, String sort, String order, Integer userId, Integer valueId) {

        PageHelper.startPage(page, limit);

        CommentExample commentExample = new CommentExample();
        commentExample.setOrderByClause(sort + " " + order);
        CommentExample.Criteria criteria = commentExample.createCriteria();
        //模糊查询条件
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }
        //只查询未删除的评论
        criteria.andDeletedEqualTo(false);
        List<Comment> comments = commentMapper.selectByExample(commentExample);

        //通过PageInfo获取分页信息
        PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);
        long total = commentPageInfo.getTotal();

        CommentListVo commentListVo = new CommentListVo();
        commentListVo.setTotal(total);
        commentListVo.setItems(comments);
        return commentListVo;

    }

    @Override
    public int delete(Comment comment) {

        comment.setDeleted(true);
        int i = commentMapper.updateByPrimaryKey(comment);
        if (i <= 0) {
            return 404;
        }
        return 200;
    }

}
